/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import controller.SearchControler;
import java.util.Iterator;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import model.Base;
import net.miginfocom.swing.MigLayout;

/**
 *
 * @author Ira
 */
public class SearchView extends JPanel{
	
	//<editor-fold defaultstate="collapsed" desc="nom des composants">
	public static final String BTN_RE_INIT = "btn_re_init";
	public static final String BTN_SEARCH = "btn_search";
	//</editor-fold>
	
	private MainWindow mainWindow;
	private SearchControler controler;
	
	private JTabbedPane tabSearchType = new JTabbedPane();
	private JTabbedPane tabDocumentSearch = new JTabbedPane();
	
	private JPanel pnlSimple = new JPanel(new MigLayout());
	private JPanel pnlAdvanced = new JPanel(new MigLayout("", "right", ""));
	private JPanel pnlUser = new JPanel(new MigLayout());
	
	private JTextField txtSimpleSearch = new JTextField(15);
	private JButton btnSearch = new JButton("Recherche");
	private JButton btnReInit = new JButton("Réinitialiser");
	
	private JCheckBox chkTitle = new JCheckBox("Titre", true);
	private JCheckBox chkAuthor = new JCheckBox("Auteur", true);
	private JCheckBox chkEditor = new JCheckBox("Editeur", true);
	private JCheckBox chkYear = new JCheckBox("Année", true);
	private JPanel pnlLimit = new JPanel(new MigLayout());
	
	private JTextField txtName = new JTextField(13);
	private JTextField txtFirstName = new JTextField(13);
	private JTextField txtPseudo = new JTextField(13);
	private JTextField txtAuthor = new JTextField(13);
	private JTextField txtEditor = new JTextField(13);
	private JTextField txtYear = new JTextField(13);
	private JTextField txtTitle = new JTextField(13);
	private JComboBox cbbCategory = new JComboBox();
	private JLabel lblAuthor = new JLabel("Auteur(s)");
	private JLabel lblEditor = new JLabel("Editeur");
	private JLabel lblYear = new JLabel("Année");
	private JLabel lblTitle = new JLabel("Titre");
	private JLabel lblCategory = new JLabel("Catégorie");
	private JLabel lblName = new JLabel("Nom");
	private JLabel lblFirstName = new JLabel("Prénom");
	private JLabel lblPseudo = new JLabel("Identifiant");
	
	private BookSearchView bookSearch = new BookSearchView();
	private NewsPaperSearchView newsPaperSearch = new NewsPaperSearchView();
	private MagazineSearchView magazineSearch = new MagazineSearchView();
	private CDSearchView cdSearch = new CDSearchView();
	private DVDSearchView dvdSearch = new DVDSearchView();
	
	private SearchField currentView;
	private boolean allFeatures;
	
//	private ButtonGroup radMenu = new ButtonGroup();
//	private JRadioButton radTypeDocument = new JRadioButton("Documents", true);
//	private JRadioButton radTypePerson = new JRadioButton("Personnes");
//	private JPanel pnlType = new JPanel(new MigLayout());
	
	public SearchView(Base model, boolean allFeatures) {
		
		this.allFeatures = allFeatures;
		this.controler = new SearchControler(model, this, allFeatures);
		
		this.setLayout(new MigLayout());
		
		TitledBorder border;

		// Nom descomposants
		this.btnReInit.setName(SearchView.BTN_RE_INIT);
		this.btnSearch.setName(SearchView.BTN_SEARCH);
		
		// Recherche simple
		this.pnlSimple.add(this.txtSimpleSearch, "wrap");
		
		border = BorderFactory.createTitledBorder("limiter à");
		this.pnlLimit.setBorder(border);
		this.pnlLimit.add(this.chkAuthor);
		this.pnlLimit.add(this.chkTitle, "wrap");
		this.pnlLimit.add(this.chkYear);
		this.pnlLimit.add(this.chkEditor);
		
		this.pnlSimple.add(this.pnlLimit);
		
		// Recherche avancée
		List<String> lCat = model.getCategories();
		for(Iterator<String> it=lCat.iterator(); it.hasNext();) {
			this.cbbCategory.addItem(it.next());
		}
		this.cbbCategory.addItemListener(controler);
		
		
		this.pnlAdvanced.add(this.lblTitle);
		this.pnlAdvanced.add(this.txtTitle, "wrap");
		this.pnlAdvanced.add(this.lblAuthor);
		this.pnlAdvanced.add(this.txtAuthor, "wrap");
		this.pnlAdvanced.add(this.lblEditor);
		this.pnlAdvanced.add(this.txtEditor, "wrap");
		this.pnlAdvanced.add(this.lblYear);
		this.pnlAdvanced.add(this.txtYear, "wrap");
		this.pnlAdvanced.add(this.lblCategory);
		this.pnlAdvanced.add(this.cbbCategory, "wrap");
		
		this.currentView = this.bookSearch;
		this.pnlAdvanced.add(this.currentView, "span");
		
		this.tabDocumentSearch.addTab("Simple", null, this.pnlSimple, "Effectuer une recherche simple");
		this.tabDocumentSearch.addTab("Avancée", null, this.pnlAdvanced, "Effectuer une recherche avancée");
		
		// Ajout des onglets
		if(allFeatures) {
			this.pnlUser.add(this.lblPseudo);
			this.pnlUser.add(this.txtPseudo, "wrap");
			this.pnlUser.add(this.lblName);
			this.pnlUser.add(this.txtName, "wrap");
			this.pnlUser.add(this.lblFirstName);
			this.pnlUser.add(this.txtFirstName, "wrap");
			
			this.tabSearchType.addTab("Documents", null, this.tabDocumentSearch, "Rechercher des documents");
			this.tabSearchType.addTab("Utilisateurs", null, this.pnlUser, "Rechercher des utilisateurs");
			
			this.add(this.tabSearchType, "span");
		}
		else {
			this.add(this.tabDocumentSearch, "span");
		}
		
		// Ajout des boutons
		this.add(this.btnReInit);
		this.add(this.btnSearch, "grow");
		
		// Listeners
		this.btnReInit.addActionListener(this.controler);
		this.btnSearch.addActionListener(this.controler);
		
	}
	
	public void reInit() {
		this.txtAuthor.setText("");
		this.txtEditor.setText("");
		this.txtSimpleSearch.setText("");
		this.txtTitle.setText("");
		this.txtYear.setText("");
		this.currentView.reInit();
		this.tabDocumentSearch.setSelectedIndex(0);
		
		if(this.allFeatures) {
			this.txtName.setText("");
			this.txtFirstName.setText("");
			this.txtPseudo.setText("");
			this.txtPseudo.requestFocus();
		}
		this.txtSimpleSearch.requestFocus();
	}
	
	public void setBookView() {
		this.pnlAdvanced.remove(this.currentView);
		this.currentView = this.bookSearch;
		this.pnlAdvanced.add(this.currentView, "span");
		this.repaint();
		this.validate();
	}
	
	public void setNewsPaperView() {
		this.pnlAdvanced.remove(this.currentView);
		this.currentView = this.newsPaperSearch;
		this.pnlAdvanced.add(this.currentView, "span");
		this.repaint();
		this.validate();
	}
	
	public void setMagazineView() {
		this.pnlAdvanced.remove(this.currentView);
		this.currentView = this.magazineSearch;
		this.pnlAdvanced.add(this.currentView, "span");
		this.repaint();
		this.validate();
	}
	
	public void setCDView() {
		this.pnlAdvanced.remove(this.currentView);
		this.currentView = this.cdSearch;
		this.pnlAdvanced.add(this.currentView, "span");
		this.repaint();
		this.validate();
	}
	
	public void setDVDView() {
		this.pnlAdvanced.remove(this.currentView);
		this.currentView = this.dvdSearch;
		this.pnlAdvanced.add(this.currentView, "span");
		this.repaint();
		this.validate();
	}
	
	public boolean isSimpleSearch() {
		return this.tabDocumentSearch.getSelectedIndex() == 0;
	}
	
	public String getSimpleSearchText() {
		return this.txtSimpleSearch.getText();
	}
	
	public boolean isTitleSearched() {
		return this.chkTitle.isSelected();
	}
	
	public boolean isAuthorSearched() {
		return this.chkAuthor.isSelected();
	}
	
	public boolean isEditorSearched() {
		return this.chkEditor.isSelected();
	}
	
	public boolean isYearSearched() {
		return this.chkYear.isSelected();
	}
	
	public boolean isSearchingUser() {
		return this.allFeatures && this.tabSearchType.getSelectedIndex() == 1;
	}

	/**
	 * @return the txtName
	 */
	public String getTxtNameText() {
		return txtName.getText();
	}

	/**
	 * @return the txtFirstName
	 */
	public String getTxtFirstName() {
		return txtFirstName.getText();
	}

	/**
	 * @return the txtPseudo
	 */
	public String getTxtPseudo() {
		return txtPseudo.getText();
	}

	/**
	 * @return the txtAuthor
	 */
	public String getTxtAuthor() {
		return txtAuthor.getText();
	}

	/**
	 * @return the txtEditor
	 */
	public String getTxtEditor() {
		return txtEditor.getText();
	}

	/**
	 * @return the txtYear
	 */
	public String getTxtYear() {
		return txtYear.getText();
	}

	/**
	 * @return the txtTitle
	 */
	public String getTxtTitle() {
		return txtTitle.getText();
	}

	/**
	 * @return the cbbCategory
	 */
	public String getCbbCategory() {
		return (String)cbbCategory.getSelectedItem();
	}
	
	public int getLenght() {
		String select = (String)cbbCategory.getSelectedItem();
		try {
			if(select.equals("Livre")) {
				return Integer.parseInt(this.bookSearch.getNbPage());
			}
			else if(select.equals("CD")) {
				return Integer.parseInt(this.cdSearch.getLenght());
			}
			else if(select.equals("DVD")) {
				return Integer.parseInt(this.dvdSearch.getLenght());
			}
		}
		catch(Exception e) {
			
		}
		return 0;
	}

	public String getType() {
		String select = (String)cbbCategory.getSelectedItem();
		if(select.equals("Livre")) {
			return this.bookSearch.getType();
		}
		else if(select.equals("CD")) {
			return this.cdSearch.getType();
		}
		else if(select.equals("DVD")) {
			return this.dvdSearch.getType();
		}
		return null;
	}

	public String getDayDate() {
		String select = (String)cbbCategory.getSelectedItem();
		if(select.equals("Journal")) {
			return this.newsPaperSearch.getDayDate();
		}
		return null;
	}
}
